<?php
declare (strict_types = 1);
namespace app\index\model\user;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserOrderRelation extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where){
        return $this->where($where)->find();
    }

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getListByWhere($where){
        $list = $this->where($where)->select();
        if($list) $list = $list->toArray();
        return $list;
    }

    /**
     * 更新记录
     * @param $id
     * @param $saveData
     * @return static
     */
    function saveData($id,$saveData){
        return $this->where('id',$id)->update($saveData);
    }

    /**
     * 添加关联记录
     * @param $userId
     * @param $orderInfo
     * @return array|int|null|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addRecord($userId,$orderInfo){
        $where = ['user_id'=>$userId,'order_id'=>$orderInfo['id']];
        $checkInfo = $this->where($where)->find();
        if($checkInfo) return $checkInfo;
        return $this->insertGetId([
            'user_id'=>$userId,
            'order_id'=>$orderInfo['id'],
            'pile_id'=>$orderInfo['pile_id'],
            'gun_id'=>$orderInfo['gun_id'],
            'day_date'=>$orderInfo['pay_time'] ? date("Y-m-d",strtotime($orderInfo['pay_time'])) : '',
            'month_date'=>$orderInfo['pay_time'] ? date("Y-m",strtotime($orderInfo['pay_time'])) : '',
        ]);
    }

}
